<template>
  <div>
    <el-card shadow="hover" class="box-card">
      <div class="form">
        <div class="mgb10">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText" class="filter-input"> </el-input>
        </div>
        <el-button type="primary" size="mini" class="baseBut" @click.stop="editMian.visible = true">新增主体</el-button>
        <el-tree
          ref="tree"
          :props="defaultProps"
          node-key="id"
          lazy
          :load="loadNode"
          :filter-node-method="filterNode"
          class="treeBody"
        >
          <div class="custom-tree-node" slot-scope="{ data }">
            <div>
              {{ data.principalName }}
              <span>（排序：{{ data.sort }}）</span>
              <span>（创建时间：{{ data.createTime }}）</span>
            </div>
            <div>
              <span>显示状态：</span>
              <el-switch
                v-model="data.status"
                class="mgr10"
                :active-value="1"
                :inactive-value="0"
                @change="switchChange(data)"
              ></el-switch>
              <el-button
                icon="el-icon-edit"
                type="warning"
                size="mini"
                @click.stop="() => editGridName(data)"
              ></el-button>
              <el-button
                v-if="data.parentId == 0"
                icon="el-icon-circle-plus-outline"
                size="mini"
                @click.stop="() => addGrid(data)"
              ></el-button>
              <el-button
                icon="el-icon-delete-solid"
                type="danger"
                size="mini"
                @click.stop="() => removeGrid(data)"
              ></el-button>
            </div>
          </div>
        </el-tree>
      </div>
    </el-card>
    <el-dialog
      :title="editMian.id === 0 ? '新增主体' : '编辑主体'"
      :visible.sync="editMian.visible"
      width="30%"
      @close="editMianClose"
    >
      <el-form label-width="100px" :model="editMian">
        <el-form-item label="主体名称：">
          <el-input v-model="editMian.principalName" placeholder="请输入主体名称"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input-number v-model="editMian.sort" placeholder="请输入排序"></el-input-number>
        </el-form-item>
        <el-form-item label="状态：">
          <el-switch v-model="editMian.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editMian.visible = false">取 消</el-button>
        <el-button type="primary" @click="editMianSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/mainBody'
export default {
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'isLeaf',
      },
      filterText: '',
      editMian: {
        id: 0,
        visible: false,
        sort: 0,
        principalName: '',
        status: 0,
        parentId: 0,
      },
      xzAddNode: null,
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    },
  },
  methods: {
    // 加载子数据
    async loadNode(node, resolve) {
      let data = await api.getChildSubject({ parentId: node?.data?.id || 0 })
      for (const item of data.data) {
        if (!item.hasChildren) {
          item.isLeaf = true
        }
      }
      return resolve(data.data)
    },
    // 筛选
    filterNode(value, data) {
      if (!value) return true
      return data.principalName.indexOf(value) !== -1
    },
    // 编辑
    editGridName(e) {
      Object.assign(this.editMian, e)
      this.editMian.visible = true
      this.xzAddNode = e
    },
    // 新增子主体类型
    addGrid(data) {
      this.editMian.parentId = data.id
      this.editMian.visible = true
      this.xzAddNode = data
    },
    // 删除行政地图
    removeGrid(data) {
      this.$confirm('此操作将永久删除该主体, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        api.reById({ id: data.id }).then(res => {
          if (res.code == '200') {
            this.$refs.tree.remove(data)
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
          }
        })
      })
    },
    // 修改显示状态
    switchChange(data) {
      let { id, status } = data
      api.updateStatus({ id, status }).then(res => {
        if (res.code == '200') {
          this.$message({
            type: 'success',
            message: '修改成功!',
          })
        }
      })
    },
    editMianClose() {
      this.$data.editMian = this.$options.data().editMian
    },
    // 提交主体编辑新增
    editMianSub() {
      if (this.editMian.id == 0) {
        api.saveType(this.editMian).then(res => {
          if (res.code == '200') {
            if (this.editMian.parentId == 0) {
              location.reload()
            } else {
              this.editMian.id = res.data
              this.editMian.isLeaf = false
              this.$refs.tree.append(this.editMian, this.xzAddNode)
              this.editMianClose()
            }
          }
        })
      } else {
        api.updateType(this.editMian).then(res => {
          if (res.code == '200') {
            this.$message({
              type: 'success',
              message: '修改成功!',
            })
            this.xzAddNode.principalName = this.editMian.principalName
            this.xzAddNode.sort = this.editMian.sort
            this.xzAddNode.createTime = this.editMian.createTime
            this.xzAddNode.status = this.editMian.status
            this.editMianClose()
          }
        })
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.treeBody {
  width: 100%;
  ::v-deep .el-tree-node__content {
    height: 40px;
  }
}
.transferBox {
  ::v-deep .el-transfer-panel {
    width: 250px;
  }
}
</style>
